package cn.tedu.charging.order.config;

import io.swagger.models.auth.In;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * rabbitmq配置
 */
@Configuration
public class RabbitMQConfiguration {

    /**
     * 正常exchange 正常队列 正常路由key
     */
    public static final String ORDER_QUEUE = "charging_order_queue";
    public static final String ORDER_ROUTING_KEY = "charging_order_routing_key";
    public static final String ORDER_EXCHANGE = "charging_order_exchange";

    /**
     * 死信 exchange 死信 队列 死信 路由key
     */
    public static final String DEAD_LETTER_EXCHANGE_NAME = "dead_letter_charging_order_exchange";
    public static final String DEAD_LETTER_ROUTING_KEY = "dead_letter_charging_order_routing_key";
    public static final String DEAD_LETTER_QUEUE_NAME = "dead_letter_charging_order_queue";



    /**
     * 通过Spring的方式创建交换机
     * Direct 是 直连交换机
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        //开启持久化
        Boolean durable = true;
        return new DirectExchange(ORDER_EXCHANGE, durable, false);
    }

    /**
     * 通过Spring的方式 创建队列
     * @return
     */
    @Bean
    public Queue orderQueue() {
        Map<String, Object> args = new HashMap<>();
        //模拟时间 设置消息的过期时间为两分钟 模拟充电充满需要两分钟
        Integer ttl = 2 * 60 * 1000;
        args.put("x-message-ttl", ttl);
        //设置正常队列消息过期后进入的死信队列Exchange
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);
        //设置正常队列消息过期后进入的死信队列RoutingKey
        args.put("x-dead-letter-routing-key", DEAD_LETTER_ROUTING_KEY);

        //开启持久化
        Boolean durable = true;

        return new Queue(ORDER_QUEUE,
                durable, false, false, args);
    }

    /**
     * 通过Spring的方式 通过 routingKey 绑定 队列 和 交换机
     * @return
     */
    @Bean
    public Binding binding() {
        return BindingBuilder
                .bind(orderQueue())
                .to(directExchange())
                .with(ORDER_ROUTING_KEY);
    }

    /**
     * 通过Spring @Bean 方式 创建 DirectExchange
     * Direct 是Exchange的一种类型
     * @return
     */
    @Bean
    public DirectExchange deadLetterDirectExchange() {
        //开启持久化
        Boolean durable = true;
        return new DirectExchange(DEAD_LETTER_EXCHANGE_NAME, durable, false);
    }

    /**
     * 通过Spring @Bean 方式 创建 Queue
     * 原生 channel.queueDeclare
     * @return
     */
    @Bean
    public Queue deadLetterOrderQueue() {
        //开启持久化
        Boolean durable = true;
        return new Queue(DEAD_LETTER_QUEUE_NAME, durable, false, false,null);
    }

    /**
     * 通过Spring @Bean 方式
     * 把Exchange 和 Queue 通过 routingKey 绑定
     * @return
     */
    @Bean
    public Binding deadLetterOrderBind() {
        return BindingBuilder.bind(deadLetterOrderQueue()).
                to(deadLetterDirectExchange()).with(DEAD_LETTER_ROUTING_KEY);
    }

}
